<?php
namespace app\api\controller\zfshop;
class Goodsagent extends Base
{

    public function __construct() {
        parent::__construct();
        $this->checkLogin();
    }

    public function lists(){
        $d = $this->post();
        $rule = [
            'page'=>'require|integer',
            'limit'=>'require|integer',
        ];
        $msg = [
        ];
        check($rule,$msg,$d);

        $map = [];
        $map[] = ["and","a.user_ext_id","=",$this->shopinfo["id"]];
        $w = where($map);
        $order = "order by a.id desc";
        $sql = "
            select
                b.id as goods_id,
                b.goods_price_id as goods_price_id,
                b.title goods_name,
                b.pic,
                b.price,
                b.point,
                b.sales_volume,
                b.gulijin_money,
                b.description,
                a.id as goods_agent_id,
                a.create_time
            from
                goods_agent a
            inner join
                goods b on b.id = a.goods_id
        $w[0] {$order} ".limit($d['page'],$d['limit']);
        $rs = $this->db->query($sql,$w[1],'',true);
        foreach ($rs[0] as $k => $v) {
            $rs[0][$k]["create_time_name"] = com_time_format($v['create_time'],'Y-m-d');
        }
        suc($rs[0],$rs[1]);
    }

    /**
     * 商品列表
     */
    public function goodsLists()
    {
        $d = $this->post();
        $rule = [
            'page' => 'require|integer',
            'limit' => 'require|integer',
        ];
        $msg = [];
        check($rule, $msg, $d);
        $map = [];
        $map[] = ["and", "status", "=", 0];
        $w = where($map);
        $order = 'order by sort desc,id desc';
        $field = ['id','title', 'price', 'pic', 'point', 'sales_volume','goods_price_id','gulijin_money'];
        $field = implode(",", $field);
        $sql = "select {$field} from goods  $w[0] and id not in(select goods_id from goods_agent where user_ext_id={$this->shopinfo["id"]}) {$order} " . limit($d['page'], $d['limit']);
        $rs = $this->db->query($sql, $w[1], '', true);
        suc($rs[0], $rs[1]);
    }

    public function del(){
        $d = $this->post();
        $rule = [
            'id'=>'require|integer',
        ];
        $msg = [
        ];

        $rs = $this->db->query("delete from goods_agent where id = ? and user_ext_id=?",[$d["id"],$this->shopinfo["id"]]);
        if($rs){
            suc("删除成功");
        }else{
            err("删除失败");
        }
    }

    public function add(){
        $d = $this->post();
        $rule = [
            'goods_id'=>'require|integer',
        ];
        $msg = [
        ];
        $data = [
            "goods_id" => $d["goods_id"],
            "user_ext_id" => $this->shopinfo["id"],
            "create_time" => time(),
        ];
        $rs = $this->db->insert("goods_agent",$data);
        if($rs){
            suc("添加成功");
        }else{
            err("添加失败");
        }
    }


}
